package com.clstu.set;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

public class LinkedHashSetExercise01 {
    public static void main(String[] args) {
        Set set = new LinkedHashSet();
        set.add(new Car("奥迪",3000000));
        set.add(new Car("法拉利",3000000));
        set.add(new Car(new String("奥迪"),3000000));
        set.add(new Car("奥巴马",3000000));
        set.add(new Car("奥迪",3000000));
        for (Object o:set
             ) {
            System.out.println(o);//添加了双向链表属性做支撑，可以使其存入和输出顺序一致
        }
    }
}
class Car{
    private String name;
    private double price;

    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Car car = (Car) o;
        return Double.compare(car.price, price) == 0 && Objects.equals(name, car.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, price);
    }
}